package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements INewsDao {
    @Override
    public int addNews(NewsModel newsModel) {
        String sql = "insert into news (title,content,create_time,news_type) values('"+ newsModel.getTitle() +
                "','"+ newsModel.getContent() +"','"+ newsModel.getCreateTime() +
                "',"+ newsModel.getNewsType().getId() +")";
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteNews(int id) {
        String sql = "delete from news where id = "+id;
        return JDBCUtils.excute(sql);
    }

    @Override
    public int editNews(NewsModel newsModel) {
        String sql = "update news set title='"+newsModel.getTitle() + "',content='"+ newsModel.getContent() +
                "',news_type="+ newsModel.getNewsType().getId() +"',update_time="+ newsModel.getUpdateTime() +
                " where id="+newsModel.getId();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NewsModel> getNewsList() {
        String sql = "select * from news";
        List<NewsModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NewsModel news = null;
        try {
            while (rs.next()){
                news = new NewsModel();
                news.setId(rs.getInt("id"));
                news.setTitle(rs.getString("title"));
                news.setContent(rs.getString("content"));
                news.setCreateTime(rs.getString("create_time"));
                news.setUpdateTime(rs.getString("update_time"));
                news.setEnable(rs.getInt("enable"));
                news.setNote(rs.getString("note"));
                news.setNewsType(new NewsTypeDaoImpl().getNewsType(rs.getInt("news_type")));
                list.add(news);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public NewsModel getNews(int id) {
        String sql = "select * from news where id="+id;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NewsModel news = null;
        try {
            while (rs.next()){
                news = new NewsModel();
                news.setId(rs.getInt("id"));
                news.setTitle(rs.getString("title"));
                news.setContent(rs.getString("content"));
                news.setCreateTime(rs.getString("create_time"));
                news.setUpdateTime(rs.getString("update_time"));
                news.setEnable(rs.getInt("enable"));
                news.setNote(rs.getString("note"));
                news.setNewsType(new NewsTypeDaoImpl().getNewsType(rs.getInt("news_type")));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return news;
    }

    @Override
    public List<NewsModel> getNewsList(int page, int size) {
        String sql = "select * from news limit "+ (page-1)*size +","+size;
        List<NewsModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NewsModel news = null;
        try {
            while (rs.next()){
                news = new NewsModel();
                news.setId(rs.getInt("id"));
                news.setTitle(rs.getString("title"));
                news.setContent(rs.getString("content"));
                news.setCreateTime(rs.getString("create_time"));
                news.setUpdateTime(rs.getString("update_time"));
                news.setEnable(rs.getInt("enable"));
                news.setNote(rs.getString("note"));
                news.setNewsType(new NewsTypeDaoImpl().getNewsType(rs.getInt("news_type")));
                list.add(news);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsModel> getNewsList(int page, int size, String sort) {
        String sql = "select * from news order by "+ sort +" limit "+ (page-1)*size +","+size;
        List<NewsModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NewsModel news = null;
        try {
            while (rs.next()){
                news = new NewsModel();
                news.setId(rs.getInt("id"));
                news.setTitle(rs.getString("title"));
                news.setContent(rs.getString("content"));
                news.setCreateTime(rs.getString("create_time"));
                news.setUpdateTime(rs.getString("update_time"));
                news.setEnable(rs.getInt("enable"));
                news.setNote(rs.getString("note"));
                news.setNewsType(new NewsTypeDaoImpl().getNewsType(rs.getInt("news_type")));
                list.add(news);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsModel> searchNews(String sql) {
        List<NewsModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NewsModel news = null;
        try {
            while (rs.next()){
                news = new NewsModel();
                news.setId(rs.getInt("id"));
                news.setTitle(rs.getString("title"));
                news.setContent(rs.getString("content"));
                news.setCreateTime(rs.getString("create_time"));
                news.setUpdateTime(rs.getString("update_time"));
                news.setEnable(rs.getInt("enable"));
                news.setNote(rs.getString("note"));
                news.setNewsType(new NewsTypeDaoImpl().getNewsType(rs.getInt("news_type")));
                list.add(news);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public long getNewsCount() {
        String sql = "select count(*) from news";
        return JDBCUtils.getCount(sql);
    }
}
